package cn.jsj.hzu.qm.facecloud.ciuser.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import cn.jsj.hzu.qm.facecloud.ciuser.dao.CilabrecordDao;
import cn.jsj.hzu.qm.facecloud.ciuser.pojo.Cilabrecord;

/**
 * 实验室安全记录表Dao
 * */
public class CilabrecordDaoImpl implements CilabrecordDao{
	
	JdbcTemplate jdbcTemplate;
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	
	//增
	/**
	 * 1.1 新增检查记录
	 * */
	@Override
	public int addCilabrecord(Cilabrecord cilabrecord) {
		String sql = "INSERT INTO tb_cilabrecord VALUES(null, ?,?,NOW(), ?)";
		try {
			int i = jdbcTemplate.update(sql, new Object[] {
					cilabrecord.getLabname(),
					cilabrecord.getRecord(),
					cilabrecord.getWho()
			});
			
			return i;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		
	}
	//删
	//（暂无）
	//改
	//（暂无）
	
	//4 查
	
	/**
	 * 4.1 查总数
	 * */
	@Override
	public int getCilabrecordsCount() {
		String sqlString = "SELECT COUNT(*) FROM tb_cilabrecord";
		
		int i = jdbcTemplate.queryForObject(sqlString, Integer.class);
		return i;
	}
	
	/**
	 * 4.2 按limit查
	 */
	@Override
	public List<Cilabrecord> getCilabrecordsByLimit(int start, int length) {
		
		String sqlString = "SELECT * FROM tb_cilabrecord ORDER BY recorddate DESC,id LIMIT ?,?";
		
		List<Cilabrecord> cilabrecords = jdbcTemplate.query(sqlString, new BeanPropertyRowMapper<>(Cilabrecord.class), new Object[] {start, length});
		return cilabrecords;
	}
	
	/**
	 * 4.3 分页查
	 * */
	@Override
	public List<Cilabrecord> getCilabrecordsByPage(int page, int perpage) {
		int sum = this.getCilabrecordsCount();
		
		int start = (page - 1) * perpage;
		int length = perpage;
		if (start >= sum) {
			return null;
		}

		if (start + perpage >= sum) {
			length = sum-start;
		}
		
		return this.getCilabrecordsByLimit(start, length);
		
	}
}
